package com.songcha.module_mine.ui.activity.browse_history

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.interfaces.IResult
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.library_business.proxy.database.BookBrowseHistoryDaoManager
import com.songcha.library_business.proxy.database.BookDetailDaoManager
import com.songcha.library_business.proxy.database.BookReadProgressDaoManager
import com.songcha.module_mine.structs.BrowseHistoryItem

class BrowseHistoryViewModel(app: Application):BaseViewModel<BrowseHistoryRepository>(app) {
    val historyList=MutableLiveData<MutableList<BrowseHistoryItem>>()

    fun getBrowseHistoryList(){
        handleDataObserver(repository!!.getBrowseHistoryList(),object : IResult<MutableList<BrowseHistoryItem>> {
            override fun onSuccess(data: MutableList<BrowseHistoryItem>) {
                historyList.value=data
            }
        },true)


        /*val browseHistoryList=BookBrowseHistoryDaoManager.getBrowseHistoryList()
        val ids= mutableListOf<String>()
        for(item in browseHistoryList){
            ids.add(item.bookId)
        }
        val detailList=BookDetailDaoManager.getBookdetailListByIdList(ids)
        val readProgressList=BookReadProgressDaoManager.getReadProgressListByIdList(ids)
        if(detailList.size==readProgressList.size){
            val tmp= mutableListOf<BrowseHistoryItem>()
            for(i in 0 until detailList.size){
                val detailItem=detailList[i]
                val readProgressItem=readProgressList[i]
                val item= BrowseHistoryItem().apply {
                    bookId=detailItem.bookId
                    author=detailItem.author
                    bookName=detailItem.name
                    thumb=detailItem.thumb
                    desc=detailItem.desc
                    textCount=detailItem.textCount
                    progress=((readProgressItem.currentChapter*1.0f/detailItem.chapterCount)*100).toInt()
                }

                tmp.add(item)
            }
            historyList.value=tmp
        }*/
    }
}